package com.projet.whattowatch.objet;

import java.util.ArrayList;

public class LibCinema
{
	private ArrayList<Cinema> listCinema;

	public LibCinema()
	{
		this.listCinema = new ArrayList<Cinema>();
	}

	public ArrayList<Cinema> getListCinema()
	{
		return this.listCinema;
	}

	public Cinema getCinema(String id)
	{
		String[] idTable = id.split("-");
		String monAdresse = idTable[0];

		for (int i = 0; i < listCinema.size(); i++)
		{
			if (listCinema.get(i).getAdresse().compareTo(monAdresse) == 0)
			{
				return listCinema.get(i);
			}
		}
		return null;
	}

	public Cinema getCinema(int index)
	{
		return listCinema.get(index);
	}

	public boolean remove(Cinema monCinema)
	{
		return listCinema.remove(monCinema);
	}

	public void addCinema(Cinema monCinema)
	{
		int debut = 0;
		int fin = this.getListCinema().size() - 1;
		recAddCinema(debut, fin, monCinema);
	}

	public void recAddCinema(int debut, int fin, Cinema monCinema)
	{
		int milieu = (fin + debut) / 2;
		if (fin < 0) {
			listCinema.add(milieu, monCinema);
		} else if (fin == 0) {
			if (monCinema.compareTo(listCinema.get(milieu)) > 0) {
				listCinema.add(milieu, monCinema);
			} else {
				listCinema.add(monCinema);
			}
		} else if (monCinema.compareTo(listCinema.get(milieu)) > 0) {
			if (debut == milieu) {
				listCinema.add(milieu, monCinema);
			} else
				recAddCinema(debut, milieu, monCinema);
		} else {
			if (milieu == debut) {
				if (monCinema.compareTo(listCinema.get(fin)) > 0) {
					listCinema.add(fin, monCinema);
				} else {
					listCinema.add(monCinema);
				}
			} else
				recAddCinema(milieu, fin, monCinema);
		}
	}
	
	public ArrayList<String> searchCinema(String keyword)
	{
		String keywordLowerCase = keyword.toLowerCase();
		ArrayList<String> resultat = new ArrayList<String>();
		for (int i = 0; i < listCinema.size(); i++) {
			String listCinemaLowerCase = listCinema.get(i).getNom().toLowerCase();
			if (listCinemaLowerCase.contains(keywordLowerCase)) {
				resultat.add(listCinema.get(i).getNom());
			}
		}
		return resultat;
	}
}